package com.miaoxun.common.file;

import com.miaoxun.common.util.Constant;
import com.miaoxun.common.util.JsonUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.*;

import java.awt.*;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * excel
 */
public class ExcelUtil {

    public static Workbook CreateExcel(List<? extends Serializable> data) {

        Workbook workbook = new HSSFWorkbook();

        if (data == null && data.size() == 0)
            return workbook;

        Sheet sheet = workbook.createSheet(Serializable.class.getName());

        for (int i = 0; i < data.size(); i++) {

            Row row0 = null;
            if (i == 0) {
                row0 = sheet.createRow(0);
            }

            Row row = sheet.createRow(i + 1);

            Serializable serializable = data.get(i);
            Field[] declaredFields = serializable.getClass().getDeclaredFields();


            for (int j = 0; j < declaredFields.length; j++) {
                Field declaredField = declaredFields[j];
                if ("serialVersionUID".equalsIgnoreCase(declaredField.getName()))
                    continue;
                Cell cell = row.createCell(j);
                cell.setCellValue(new HSSFRichTextString(getValueByField(declaredField, serializable)));

                if (row0 != null) {
                    Cell cell0 = row0.createCell(j);
                    cell0.setCellValue(declaredField.getName());

                }
            }
        }
        return workbook;
    }

    public static List<? extends Serializable> CreateData(InputStream inputStream, Class clazz) {

        List<Map<String, String>> list = new ArrayList<>();

        Workbook workbook = null;
        try {
            workbook = new HSSFWorkbook(new POIFSFileSystem(inputStream));
        } catch (IOException e) {
            System.out.println(e);
            e.printStackTrace();
        }


        Sheet sheet = workbook.getSheetAt(0);
        Row row0 = sheet.getRow(0);

        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            Map<String, String> map = new HashMap<>();
            for (int j = 0; j < row.getLastCellNum(); j++) {

                Cell cell0 = row0.getCell(j);

                Cell cell = row.getCell(j);
                if (cell == null || cell0 == null)
                    continue;
                String s = "-1";
                if (cell.getStringCellValue() != null && !cell.getStringCellValue().equals("") && !"null".equalsIgnoreCase(cell.getStringCellValue()))
                    s = cell.getStringCellValue();

                map.put(cell0.getStringCellValue(), s);
            }
            list.add(map);
        }


        String s = JsonUtil.toJSONString(list);

        return JsonUtil.parseArray(s, clazz);
    }


    public static String getValueByField(Field field, Object object) {
        field.setAccessible(true);
        try {


            String s = new String(String.valueOf(field.get(object)).getBytes(), getSystemFileCharset());
            return s;


        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return e.getMessage();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return e.getLocalizedMessage();
        }
    }

    public static String getSystemFileCharset() {
        Properties pro = System.getProperties();
        return pro.getProperty("file.encoding");
    }
}
